package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-10-30 18:41
 **/
public class LeetCode55 {

    /*public boolean canJump(int[] nums) {
        int size = nums.length;
        //f[i]表示i索引位置是否可达
        boolean[] f = new boolean[size];
        f[0] = true;

        for (int i = 1; i < size; i++) {
            f[i] = false;
            //判断[0,i)区间中的索引是否可以跳到i处
            for (int i1 = 0; i1 < i; i1++) {
                //如果i1索引可达，且nums[i1] + i1 >= i 说明i索引可达
                if (f[i1] && nums[i1] + i1 >= i){
                    f[i] = true;
                }
            }
        }
        //返回最后一个下标是否可达
        return f[size - 1];
    }*/

    public boolean canJump(int[] nums) {
        int reach = 0;
        for (int i = 0; i <= reach && reach < nums.length - 1; i++) {
            reach = Math.max(reach, nums[i] + i);
        }
        return reach >= nums.length - 1;
    }
}
